package com.itheima.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.FeedbackMapper;
import com.itheima.dao.UserMapper;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Feedback;
import com.itheima.pojo.User;
import com.itheima.service.FeedbackService;
import com.itheima.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FeedbackServiceImpl implements FeedbackService {

    @Autowired
    private FeedbackMapper feedbackMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private IdWorker idWorker;

    //获取反馈列表
    @Override
    public PageResult<Feedback> findAll(Map<String, Object> feedbackVO) {
        Integer currentPage = Integer.parseInt(feedbackVO.get("currentPage").toString());
        Integer pageSize = Integer.parseInt(feedbackVO.get("pageSize").toString());
        PageHelper.startPage(currentPage,pageSize);
        Example example=this.createExample(feedbackVO);
        Page<Feedback> feedbackPage = (Page<Feedback>) feedbackMapper.selectByExample(example);
        List<Feedback> feedbackList = feedbackPage.getResult();
        for (Feedback feedback : feedbackList) {
            String uid = feedback.getUserUid();
            User user = userMapper.selectByPrimaryKey(uid);
            if (user!=null){
                feedback.setUser(user);
            }
        }
        PageResult<Feedback> pageResult=new PageResult<>();
        pageResult.setTotal(feedbackPage.getTotal());
        pageResult.setRecords(feedbackPage);
        pageResult.setSize((long)pageSize);
        pageResult.setCurrent((long) currentPage);
        pageResult.setOptimizeCountSql(true);
        pageResult.setSearchCount(true);
        return pageResult;
    }

    //编辑反馈
    @Override
    @Transactional
    public void edit(Map<String, Object> feedbackVO) {
        //将map转为comment对象
        String feedbackStr = JSON.toJSONString(feedbackVO);
        Feedback feedback = JSON.parseObject(feedbackStr, Feedback.class);
        //将修改时间添加到对象
        feedback.setUpdateTime(new Date());
        //修改操作
        feedbackMapper.updateByPrimaryKey(feedback);
    }

    //批量删除反馈
    @Override
    @Transactional
    public void deleteBatch(List<Map<String, Object>> feedbackVOList) {
        for (Map<String, Object> map : feedbackVOList) {
            String uid = map.get("uid").toString();
            feedbackMapper.updateByUid(uid);
        }
    }

    //添加反馈
    @Override
    public void add(Map<String, Object> feedbackVO) {
        //将map转为comment对象
        String feedbackStr = JSON.toJSONString(feedbackVO);
        Feedback feedback = JSON.parseObject(feedbackStr, Feedback.class);
        //添加创建和修改时间
        feedback.setUid(String.valueOf(idWorker.nextId()));
        feedback.setCreateTime(new Date());
        feedback.setUpdateTime(new Date());
        feedbackMapper.insertSelective(feedback);
    }

    //获取用户反馈
    @Override
    public List<Feedback> findByUserUid(String userUid) {
        Map<String,Object> map=new HashMap<>();
        map.put("userUid",userUid);
        Example example = this.createExample(map);
        List<Feedback> feedbackList = feedbackMapper.selectByExample(example);
        return feedbackList;
    }

    /**
     * 构建查询对象
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(Feedback.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // 唯一uid
            if(searchMap.get("uid")!=null && !"".equals(searchMap.get("uid"))){
                criteria.andEqualTo("uid",searchMap.get("uid"));
            }
            // 用户uid
            if(searchMap.get("userUid")!=null && !"".equals(searchMap.get("userUid"))){
                criteria.andEqualTo("userUid",searchMap.get("userUid"));
            }
            // 反馈的内容
            if(searchMap.get("content")!=null && !"".equals(searchMap.get("content"))){
                criteria.andLike("content","%"+searchMap.get("content")+"%");
            }

            // 状态
            if(searchMap.get("status")!=null){
                criteria.andEqualTo("status",searchMap.get("status"));
            }

            // 创建时间
            if(searchMap.get("createTime")!=null){
                criteria.andEqualTo("createTime",searchMap.get("createTime"));
            }

            // 更新时间
            if(searchMap.get("updateTime")!=null){
                criteria.andEqualTo("updateTime",searchMap.get("updateTime"));
            }

            // 标题
            if(searchMap.get("title")!=null && !"".equals(searchMap.get("title"))){
                criteria.andEqualTo("title",searchMap.get("title"));
            }

            // 回复
            if(searchMap.get("reply")!=null && !"".equals(searchMap.get("reply"))){
                criteria.andEqualTo("reply",searchMap.get("reply"));
            }

            // 反馈状态： 0：已开启  1：进行中  2：已完成  3：已拒绝
            if(searchMap.get("feedbackStatus")!=null){
                criteria.andEqualTo("feedbackStatus",searchMap.get("feedbackStatus"));
            }

            // 管理员uid
            if(searchMap.get("adminUid")!=null && !"".equals(searchMap.get("adminUid"))){
                criteria.andEqualTo("adminUid",searchMap.get("adminUid"));
            }

        }
        return example;
    }
}
